<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <input type="hidden" name="pid" value="{$find.pid}">
    <input type="hidden" name="status" value="{$find.status}">
    <div class="layui-form-item">
        <label class="layui-form-label">活动名称</label>
        <div class="layui-input-inline" style="width: 400px;">
            <input type="text" class="layui-input" name="name" value="{$find.name}" lay-verify="required" placeholder="活动名称" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">打卡时间</label>
        <div class="layui-input-inline" style="width: 400px;">
            <input type="radio" name="tian" value="3" title="3天" {if condition="$find.tian == '3'"}checked="checked"{elseif condition="!$find"/}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
            <input type="radio" name="tian" value="7" title="7天" {if condition="$find.tian == '7'"}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
            <input type="radio" name="tian" value="14" title="14天" {if condition="$find.tian ==
            '14'"}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
            <input type="radio" name="tian" value="21" title="21天" {if condition="$find.tian == '21'"}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
            <input type="radio" name="tian" value="60" title="60天" {if condition="$find.tian == '60'"}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品选择</label>
        <div class="layui-input-inline" style="width: 400px;">
            <input type="text" class="layui-input" value="{$find.shop}" ts-selected="{$find.pid}" id="demo" lay-verify="required" placeholder="请选择商品" autocomplete="off" {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品价格</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="price" value="{$find.price}" lay-verify="required|number" placeholder="商品价格" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品邮费</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="freight" value="{$find.freight}" lay-verify="required|number" placeholder="商品邮费" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">打卡海报</label>
        <div class="layui-input-inline">
            <input type="text" name="img" value="{$find.img}" placeholder="请上传打卡海报" autocomplete="off" class="layui-input">
        </div>
        <button style="float: left;" type="button" class="layui-btn" id="up">上传海报</button>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称颜色</label>
        <div class="layui-input-inline" style="width: 120px;">
            <input type="text" name="color" value="{$find.color}" placeholder="请选择颜色" class="layui-input" id="test-form-input">
        </div>
        <div class="layui-inline" style="left: -11px;">
            <div id="test-form"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">打卡规则</label>
        <div class="layui-input-inline" style="width: 600px;">
            <input type="radio" name="rule" value="1" title="早上 7-8 点" {if condition="$find.rule == '1'"}checked="checked"{elseif condition="!$find"/}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
            <input type="radio" name="rule" value="2" title="晚上 8-9 点" {if condition="$find.rule == '2'"}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
            <input type="radio" name="rule" value="3" title="早上 7-8 点 和 晚上 8-9 点" {if condition="$find.rule == '3'"}checked="checked"{/if} {if condition="$find && $find.status eq 1"}disabled="disabled"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">退回押金</label>
        <div class="layui-input-inline">
            <input type="checkbox" name="tui" value="1" lay-skin="switch" lay-text="开启|关闭" {if condition="$find.tui == '1'"}checked="checked"{elseif condition="!$find"/}checked="checked"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">参与人数</label>
        <div class="layui-input-inline">
            <input type="text" name="num" value="{$find.num}" lay-verify="required|number" placeholder="参与人数" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">打卡提醒</label>
        <div class="layui-input-inline" style="width: 400px;">
            <textarea class="layui-textarea" name="remind">{$find.remind}</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">规则说明</label>
        <div class="layui-input-block">
            <script id="explain" type="text/plain">
                {$find.explain}
            </script>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="提交">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="修改">
    </div>
    <script type="text/html" id="time">
        {{ formatDateTime(d.time) }}
    </script>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<!-- 配置文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="/static/js/jquery-3.1.1.min.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'laydate', 'form', 'tableSelect', 'upload', 'colorpicker'], function(){
        var $ = layui.$,
                form = layui.form,
                laydate = layui.laydate,
                upload = layui.upload,
                tableSelect = layui.tableSelect,
                colorpicker = layui.colorpicker;

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            ,color: '#ffffff'
            ,done: function(color){
                $('#test-form-input').val(color);
            }
        });

        //编辑器
        var explain = UE.getEditor('explain', {
            initialFrameWidth: 800,
            initialFrameHeight: 320
        });

        //普通图片上传
        upload.render({
            elem: '#up'
            ,url: "{:url('Activity/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("input[name=img]").val(res.img);
            }
        });

        tableSelect.render({
            elem: '#demo',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'name',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索商品名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('Product/product')}",
                method: 'post',
                cols: [[
                    {type: 'radio'},
                    {field: 'name', title: '商品名称', width: 400},
                    {field: 'stock', title: '库存', width: 100},
                    {field: 'volume', title: '销量', width: 100},
                    {field: 'time', title: '添加时间', width: 200, templet: "#time"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                $("input[name=pid]").val(field[0].id);
                $("#demo").val(field[0].name);
            }
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            field.explain = explain.getContent();
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Activity/clock_add')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('添加成功', {offset: ['30%', '40%']});
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('添加失败', {offset: ['30%', '40%']});
                    }
                }
            });
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            field.explain = explain.getContent();
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Activity/clock_edit')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('修改成功', {offset: ['30%', '40%']});
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('修改失败', {offset: ['30%', '40%']});
                    }
                }
            });
        });
    })

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }

    //补零
    function returnFloat(value){
        value = Math.round(parseFloat(value)*100)/100;
        var xsd = value.toString().split(".");
        if(xsd.length == 1){
            value = value.toString()+".00";
            return value;
        }
        if(xsd.length>1){
            if(xsd[1].length<2){
                value = value.toString()+"0";
            }
            return value;
        }
    }
</script>
</body>
</html>